﻿@model CML.Intercamber.Business.Users

<script>
    var basicInfoEditor = new function () {
        var texts = {
            Error_PasswordNotMatch: "@Resources.Intercamber.Error_PasswordNotMatch",
            Error_PasswordTooSimple: "@Resources.Intercamber.Error_PasswordTooSimple",
            Error_PasswordNeeded: "@Resources.Intercamber.Error_PasswordNeeded"
        };

        this.init = function () {
            $("#BirthDate").datepicker({
                showOn: "", //"button",
                buttonImage: "/Images/Calendar.png",
                buttonImageOnly: true,
                dateFormat: "dd/mm/yy"
            }).attr("aria-label", "dd/mm/yy").attr("placeholder", "dd/mm/yy");            
        };

        this.validatePassword = function (requireFeed) {
            var pwd1 = $("#PasswordInput").val();
            var pwd2 = $("#PasswordConfirm").val();
            if (pwd1 != pwd2)
                return texts.Error_PasswordNotMatch;
            if (pwd1.length > 0 && pwd1.length < 6)
                return texts.Error_PasswordTooSimple;
            if (pwd1.length == 0 && requireFeed)
                return texts.Error_PasswordNeeded;
            $("#Password").val(pwd1 != "" ? Sha256(pwd1) : "");
            return null;
        };
    };

    var scholarLevelEditor = new function () {
        this.initEditor = function () {
            $('#ScholarLevel').qtip({
                content: { text: $("#ttScholarLevel"), title: { text: "", button: true } },
                position: { at: "right center", my: "left center", viewport: $(window), adjust: { method: "shift", x: 0, y: 0 }, container: $('form') },
                style: { classes: "qtip-bootstrap" },
                show: { event: 'click', effect: false },
                hide: 'unfocus'
            });
            $(".selectableTtLangLevel").click(function () {
                $("#ScholarLevel").val($(this).html());
                $('#ScholarLevel').qtip('hide');
            });
        };
    };

    var learntLanguagesEditor = new function () {
        this.initTable = function () {
            $("#hideAddLanguage").click(hideToolTip);
            $("#okAddLanguage").click(okAddLanguage);
            $("#learnLanguagesAdd").button().click(addLanguage);

            for (var i = 0; i < listLearntLanguages.length; i++)
                printRow(listLearntLanguages[i], i);
            refreshBtn();
        };

        function printRow(el, idx) {
            var r, c;
            r = document.createElement("tr");
            c = document.createElement("td");
            c.innerHTML = "<img class='imgButton deleteLearnLanguage' lang='" + el.IdLanguage + "' id='deleteLearnLanguage_" + el.IdLanguage + "' src='../Images/Delete.png' />";
            r.appendChild(c);
            c = document.createElement("td");
            c.innerHTML = langLabel(el.IdLanguage) + "<input type='hidden' name='user.UsersLearnLanguages[" + idx + "].IdLanguage' value='" + el.IdLanguage + "'/>";
            r.appendChild(c);
            c = document.createElement("td");
            c.innerHTML = languageDdl(el.LanguageLevel, idx);
            r.appendChild(c);
            c = document.createElement("td");
            c.innerHTML = "<label><input type='checkbox' class='learnLangCb' name='user.UsersLearnLanguages[" + idx + "].displayInContactRequests' " + (el.displayInContactRequests == true ? "checked" : "") + " value='true'/>  @Resources.Intercamber.Profil_VisibleConcactSearch <img src='../Images/Warning.png' class='langHiddenWarn' " + (el.displayInContactRequests == true ? "style='display:none'" : "") + " /> </label>";
            r.appendChild(c);
            $("#tblLearnLanguages")[0].appendChild(r);
            $(".deleteLearnLanguage").click(delLanguage);
            $(".learnLangCb").change(function () {
                var img = $(this).parent().find("img");
                if ($(this).is(':checked'))
                    $(img).hide();
                else
                    $(img).show();
            });
            $(".langHiddenWarn").qtip({
                content: { text: "@Resources.Intercamber.Profil_NotVisibleSearch" },
                position: { at: "right center", my: "left center", viewport: $(window), adjust: { method: "shift", x: 0, y: 0 }, container: $('form') },
                style: { classes: "qtip-bootstrap" }
            });
            $(".learnLanguageLevel").qtip({
                content: { text: "@Html.Raw(Resources.Intercamber.Profil_LevelInformation.Replace("\n", "<br/>").Replace("\r", ""))" },
                position: { at: "right center", my: "left center", viewport: $(window), adjust: { method: "shift", x: 0, y: 0 }, container: $('form') },
                style: { classes: "qtip-bootstrap" }
            });
        }

        function refreshBtn() {
            if (listLearntLanguages.length == 1)
                $(".deleteLearnLanguage").hide();
            else
                $(".deleteLearnLanguage").show();
            if (listLearntLanguages.length == 3)
                $("#learnLanguagesAdd").hide();
            else
                $("#learnLanguagesAdd").show();
        }

        function languageDdl(languageLevel, idx) {
            return "<select name='user.UsersLearnLanguages[" + idx + "].LanguageLevel' class='learnLanguageLevel'>"
                + "<option value=''> </option>"
                + "<option value='A' " + (languageLevel == "A" ? "selected" : "") + ">A</option>"
                + "<option value='B' " + (languageLevel == "B" ? "selected" : "") + ">B</option>"
                + "<option value='C' " + (languageLevel == "C" ? "selected" : "") + ">C</option>"
                + "</select>";
        }

        var ttAdd = null;

        function addLanguage() {
            if (ttAdd == null) {
                ttAdd = $('<div />');
                ttAdd.qtip({
                    show: {
                        effect: false,
                        modal: { on: true, blur: false }
                    },
                    content: $('#ttAdd'),
                    position: { my: 'center', at: 'center', target: $(window), viewport: $(window), adjust: { method: "shift", x: 0, y: 0 }, effect: false },
                    events: {
                        show: function () {
                            $(this).draggable();
                            $(this).qtip('option', { 'content.title': "@Resources.Intercamber.Profil_AddLearnLanguage", 'content.title.button': 'true' });
                        },
                        hide: function () {
                            $(this).qtip('option', { 'position.my': 'top left', 'position.target': [$(this).offset().left, $(this).offset().top] });
                        }
                    },
                    hide: false,
                    style: { classes: "qtip-bootstrap qtip-rounded tipWidth", tip: false }
                });
            }
            $("#ddlTtAdd").val("");
            $("#ddlTtAdd option").removeAttr("disabled");
            for (var i = 0; i < listLearntLanguages.length; i++)
                $("#ddlTtAdd option[value=" + listLearntLanguages[i].IdLanguage + "]").attr("disabled", "disabled");

            $("#addContactMessage").val("");
            ttAdd.qtip('show');
        }

        function hideToolTip() {
            ttAdd.qtip("hide");
            return false;
        }

        function okAddLanguage() {
            var selLang = $("#ddlTtAdd").val();
            if (selLang !== "") {
                var newEl = { "IdLanguage": selLang, "displayInContactRequests": true, "LanguageLevel": null };
                listLearntLanguages[listLearntLanguages.length] = newEl;
                printRow(newEl, listLearntLanguages.length - 1);
                refreshBtn();
            }
            ttAdd.qtip("hide");
            return false;
        }

        function delLanguage() {
            var lang = $(this).attr("lang"), idx = -1, i;
            for (i = 0; i < listLearntLanguages.length; i++) {
                var el = listLearntLanguages[i];
                if (el.IdLanguage == lang) {
                    idx = i;
                    break;
                }
            }
            if (idx >= 0) {
                listLearntLanguages.splice(idx, 1);
                $("#tblLearnLanguages")[0].deleteRow(idx);
                for (i = idx + 1; i < listLearntLanguages.length + 1; i++) {
                    $("input[name=\"user.UsersLearnLanguages[" + i + "].IdLanguage\"]").attr("name", "user.UsersLearnLanguages[" + (i - 1) + "].IdLanguage");
                    $("select[name=\"user.UsersLearnLanguages[" + i + "].LanguageLevel\"]").attr("name", "user.UsersLearnLanguages[" + (i - 1) + "].LanguageLevel");
                    $("input[name=\"user.UsersLearnLanguages[" + i + "].displayInContactRequests\"]").attr("name", "user.UsersLearnLanguages[" + (i - 1) + "].displayInContactRequests");
                }

                refreshBtn();
            }
        }
    };

</script>

<table style="width: 100%;" cellpadding="4" cellspacing="4">
    <tr>
        <td class="cellTitle" style="width: 120px">@Resources.Intercamber.FirstName</td>
        <td style="width: 200px;">
            <input type="text" name="user.FirstName" id="FirstName" value="@Model.FirstName" maxlength="200" />
        </td>
        <td class="cellTitle">@Resources.Intercamber.LastName</td>
        <td>
            <input type="text" name="user.LastName" id="LastName" value="@Model.LastName" maxlength="200"/>
        </td>
    </tr>
    <tr>
        <td class="cellTitle" style="width: 120px;">@Resources.Intercamber.BirthDate</td>
        <td>
            <input type="text" name="user.BirthDate" id="BirthDate" value="@(Model.BirthDate == null ? "" : Model.BirthDate.Value.ToString("dd/MM/yyyy"))" />
        </td>
        <td class="cellTitle">@Resources.Intercamber.City</td>
        <td>
            <input type="text" name="user.City" id="City" value="@Model.City"/>
        </td>
    </tr>
    <tr>
        <td class="cellTitle">@Resources.Intercamber.Password</td>
        <td>
            <input type="password" id="PasswordInput" value="" />
            <input id="Password" name="user.Password" type="hidden" runat="server" />
        </td>
        <td class="cellTitle">@Resources.Intercamber.PasswordConfirm</td>
        <td>
            <input type="password" id="PasswordConfirm" value="" />
        </td>
    </tr>
    <tr>
        <td class="cellTitle">@Resources.Intercamber.Gender</td>
        <td>
            <select name="user.IdGender" id="IdGender">
                <option value="1" @Html.Raw(Model.IdGender == 1 ? "selected='selected'" : "")>@Resources.Intercamber.Gender_Boy</option>
                <option value="2" @Html.Raw(Model.IdGender == 2 ? "selected='selected'" : "")>@Resources.Intercamber.Gender_Girl</option>
            </select>
        </td>
        <td class="cellTitle">@Resources.Intercamber.Nationality</td>
        <td>
            <input type="text" name="user.Nationality" id="Nationality" value="@Model.Nationality" maxlength="200"/>
        </td>
    </tr>
    @if (Model.Establishments != null)
    {
        <tr>
            <td colspan="4" class="subTitle">@Resources.Intercamber.Profil_Scolarship</td>
        </tr>
        <tr>
            <td colspan="4">
                @Resources.Intercamber.Establishment: @Model.Establishments.LblEstablishment - @Model.Establishments.City (@CML.Intercamber.Web.Helpers.ResourcesHelper.GetString("Countries_" + Model.Establishments.IdCountry))
            </td>
        </tr>
    }
    @if (Model.Establishments != null)
    {
        <tr>
            <td colspan="4">
                @Resources.Intercamber.Profil_CurrentScholarLevel
                <input type="text" name="user.ScholarLevel" id="ScholarLevel" value="@Model.ScholarLevel" maxlength="200" style="width: 200px;"/>
                <div id="ttScholarLevel" style="display: none;">
                    @{
        string[] listSdFrench = Resources.Intercamber.Profil_SchoolDegree_French.Split(';');
        string[] listSdEnglish = Resources.Intercamber.Profil_SchoolDegree_English.Split(';');
        string[] listSdSpanish = Resources.Intercamber.Profil_SchoolDegree_Spanish.Split(';');
        int nbRows = Math.Max(listSdFrench.Count(), Math.Max(listSdEnglish.Count(), listSdSpanish.Count()));
                    }
                    <table cellspacing="1" cellpadding="2">
                        <tr class="ui-widget-header">
                            <td class='titleTtLangLevel'>@Resources.Intercamber.Languages_fr</td>
                            <td class='titleTtLangLevel'>@Resources.Intercamber.Languages_en</td>
                            <td class='titleTtLangLevel'>@Resources.Intercamber.Languages_es</td>
                        </tr>
                        @{
        for (int i = 0; i < nbRows; i++)
        {
            var fr = listSdFrench.Count() > i ? listSdFrench[i] : "";
            var en = listSdEnglish.Count() > i ? listSdEnglish[i] : "";
            var es = listSdSpanish.Count() > i ? listSdSpanish[i] : "";
                            <tr>
                                <td class='itemTtLangLevel @(!string.IsNullOrEmpty(fr) && Model.Establishments.IdCountry == "fr" ? "selectableTtLangLevel" : "")'>@fr</td>
                                <td class='itemTtLangLevel @(!string.IsNullOrEmpty(en) && Model.Establishments.IdCountry == "en" ? "selectableTtLangLevel" : "")'>@en</td>
                                <td class='itemTtLangLevel @(!string.IsNullOrEmpty(es) && Model.Establishments.IdCountry == "es" ? "selectableTtLangLevel" : "")'>@es</td>
                            </tr>
        }
                        }
                    </table>
                </div>
            </td>
        </tr>

        <tr>
            <td colspan="4">
                @Resources.Intercamber.Profil_LearntLanguages:
                <table>
                    <tr>
                        <td><span id="learnLanguagesAdd">@Resources.Intercamber.Add</span></td>
                        <td>
                            <table id="tblLearnLanguages" style="border: 1px solid lightgray" cellpadding="2"></table>
                        </td>
                    </tr>
                </table>

                <div id="ttAdd" style="display: none">
                    <select id="ddlTtAdd">
                        <option value="" selected></option>
                        <option value="en">@Resources.Intercamber.Languages_en</option>
                        <option value="fr">@Resources.Intercamber.Languages_fr</option>
                        <option value="es">@Resources.Intercamber.Languages_es</option>
                    </select>
                    <br />
                    <br />
                    <div class="ttButtons">
                        <button id="okAddLanguage" class="jqbutton">@Resources.Intercamber.Add</button>
                        &nbsp;
                        <button id="hideAddLanguage" class="jqbutton">@Resources.Intercamber.Cancel</button>
                    </div>
                </div>
            </td>
        </tr>
    }
</table>
